﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;
using com.lover.astd.common.model.building;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.logicexe.economy
{
    public class MovableExe : ExeBase
    {
        private int _weave_price = 130;
        private string _refine_weapone_name = "";
        private string _movable_order = "";
        private int _refine_reserve = 10;
        private List<int> _movableSerial = new List<int>();
        public MovableExe() { _name = ConfigStrings.S_Movable; _readable = ConfigStrings.SR_Movable; }
        public override long execute()
        {
            Dictionary<string, string> config = getConfig();
            if (!config.ContainsKey(ConfigStrings.enabled) || !config[ConfigStrings.enabled].ToLower().Equals("true")) return next_hour();
            if (config.ContainsKey(ConfigStrings.weave_price)) Int32.TryParse(config[ConfigStrings.weave_price], out _weave_price);
            if (config.ContainsKey(ConfigStrings.refine_reserve)) Int32.TryParse(config[ConfigStrings.refine_reserve], out _refine_reserve);

            if (config.ContainsKey(ConfigStrings.refine_item)) _refine_weapone_name = config[ConfigStrings.refine_item];
            else _refine_weapone_name = "无敌将军炮";
            if (config.ContainsKey(ConfigStrings.order)) _movable_order = config[ConfigStrings.order];
            else _movable_order = "2341";

            bool winter_trade = config.ContainsKey(ConfigStrings.winter_trade) && config[ConfigStrings.winter_trade].ToLower().Equals("true");
            bool arch_trade = false, arch_trade_max = false;
            if (_user.isActivityRunning(ActivityType.ArchEvent))
            {
                arch_trade = config.ContainsKey(ConfigStrings.arch_trade) && config[ConfigStrings.arch_trade].ToLower().Equals("true");
                arch_trade_max = config.ContainsKey(ConfigStrings.arch_trade_max) && config[ConfigStrings.arch_trade_max].ToLower().Equals("true");
            }
            string visit_merchants = ""; int max_fail_count = 0;
            if(config.ContainsKey(ConfigStrings.trade_visit)) visit_merchants = config[ConfigStrings.trade_visit];
            if (config.ContainsKey(ConfigStrings.trade_visit_fail)) Int32.TryParse(config[ConfigStrings.trade_visit_fail], out max_fail_count);

            bool newTrade = _user._is_new_trade;
            MiscMgr mgr = ServiceFactory.getMiscManager();

            bool do_trade = false, do_weave = false;
            if (_user.Level >= 40 && (_movable_order.Contains("1") || _movable_order.Contains("5") || (_user.Season == 4 && winter_trade) || arch_trade)) do_trade = true;
            if (_user.Level >= 82 && (_movable_order.Contains("2") || _movable_order.Contains("6"))) do_weave = true;

            // 如果不做纺织或通商, 那么只做免费的, 如果做的话, 那么继续向下走
            int trade_state = 0, weave_state = 0;
            if (!do_trade)
            {
                // 只有老版通商有免费通商
                if (!newTrade) mgr.handleTradeInfo(_proto, _logger, _user, out trade_state);
            }
            if (!do_weave) mgr.handleWeaveInfo(_proto, _logger, _user, _weave_price, out weave_state); 
            
            long result = -1;

            if (_user.Season == 4 && winter_trade)
            {
                // 只有老版才需要冬天先做通商
                if (!newTrade) result = mgr.handleTradeInfo(_proto, _logger, _user, out trade_state);
            }
            if (arch_trade)
            {
                // 考古先做通商通商
                // 只有老版才需要max这个参数
                if (newTrade)
                {
                    result = mgr.handleNewTradeInfo(_proto, _logger, _user, visit_merchants, max_fail_count, 
                                    getSilverAvailable(), getGoldAvailable());
                    // 貌似通商了就干不了其他了? 新版通商没有疲劳啊
                    if (result == 1 || result == 10) return next_halfhour();
                    else if (result == 2) return next_hour();
                    else if (result == 0) return immediate();
                    else return next_halfhour();
                }
                else
                {
                    if (arch_trade_max)
                    {
                        result = mgr.handleTradeInfo(_proto, _logger, _user, out trade_state, false, false);
                        if (result == 1 || result == 10) { return next_halfhour(); }
                        if (result == 3) { return next_halfhour(); }
                        if (result == 2) // 已经是疲劳或者极限了, state==2|3了
                        {
                            if (trade_state == 2)
                            {
                                // 判断驿站是否已经满了, 如果满了, 那么直接继续疲劳通商, 否则升级然后通商
                                Building b = ServiceFactory.getBuildingManager().getBuilding(_user._buildings, "驿站");
                                if (b != null && b.Level < _user.Level)
                                {
                                    bool upgrade_ok = ServiceFactory.getBuildingManager().upgradeBuilding(_proto, _logger, b, _user._buildingLines);
                                    if (upgrade_ok)
                                    {
                                        ServiceFactory.getMiscManager().getExtraInfo2(_proto, _logger, _user);
                                        return immediate();
                                    }
                                    else
                                    {
                                        logInfo("升级驿站失败, 等待半小时");
                                        return 30 * 60 * 1000;
                                    }
                                }
                                else
                                {
                                    result = mgr.handleTradeInfo(_proto, _logger, _user, out trade_state, true, false);
                                    if (result == 1 || result == 10) { return next_halfhour(); }
                                    if (result == 3) { return next_halfhour(); }
                                }
                            }
                        }
                        if (result == 0) return immediate();
                    }
                    else
                    {
                        result = mgr.handleTradeInfo(_proto, _logger, _user, out trade_state, false, false);
                        if (result == 1 || result == 10) { return next_halfhour(); }
                        if (result == 3) { return next_halfhour(); }
                        if (result == 0) return immediate();
                    }
                }
            }

            _movableSerial.Clear();
            for (int i = 0, len = _movable_order.Length; i < len; i++)
            {
                char ch = _movable_order[i];
                if (ch.Equals('1'))
                {
                    if (!_movableSerial.Contains(1)) _movableSerial.Add(1);
                }
                else if (ch.Equals('2'))
                {
                    if (!_movableSerial.Contains(2)) _movableSerial.Add(2);
                }
                else if (ch.Equals('3'))
                {
                    if (!_movableSerial.Contains(3)) _movableSerial.Add(3);
                }
                else if (ch.Equals('4'))
                {
                    if (!_movableSerial.Contains(4)) _movableSerial.Add(4);
                }
                else if (ch.Equals('5'))
                {
                    if (!_movableSerial.Contains(5)) _movableSerial.Add(5);
                }
                else if (ch.Equals('6'))
                {
                    if (!_movableSerial.Contains(6)) _movableSerial.Add(6);
                }
                else if (ch.Equals('7'))
                {
                    if (!_movableSerial.Contains(7)) _movableSerial.Add(7);
                }
                else if (ch.Equals('8'))
                {
                    if (!_movableSerial.Contains(8)) _movableSerial.Add(8);
                }
                if (_movableSerial.Count >= 4) break;
            }
            
            //1=通商,2=纺织,3=炼制,4=精炼, 5=极限通商,6=极限纺织,7=极限炼制,8=极限精炼
            foreach (int thingid in _movableSerial)
            {
                if (thingid == 2)
                {
                    result = mgr.handleWeaveInfo(_proto, _logger, _user, _weave_price, out weave_state, false, false);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 4) return next_halfhour(); // price not match
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
                else if (thingid == 6)
                {
                    result = mgr.handleWeaveInfo(_proto, _logger, _user, _weave_price, out weave_state, true, false);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 4) return next_halfhour(); // price not match
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
                else if (thingid == 3)
                {
                    result = mgr.handleRefineFactoryInfo(_proto, _logger, _user, _refine_weapone_name, _user.Silver, false);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 4) return next_halfhour(); // not enough silver
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
                else if (thingid == 7)
                {
                    result = mgr.handleRefineFactoryInfo(_proto, _logger, _user, _refine_weapone_name, _user.Silver, true);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 4) return next_halfhour(); // not enough silver
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
                else if (thingid == 4)
                {
                    result = mgr.handleRefineInfo(_proto, _logger, _user, getGemPrice(), getGoldAvailable(), _refine_reserve, false);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 4) return 60 * 1000; // 精炼cd 1分钟
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
                else if (thingid == 8)
                {
                    result = mgr.handleRefineInfo(_proto, _logger, _user, getGemPrice(), getGoldAvailable(), _refine_reserve, true);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 4) return 60 * 1000; // 精炼cd 1分钟
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
                else if (thingid == 1)
                {
                    if (newTrade)
                    {
                        result = mgr.handleNewTradeInfo(_proto, _logger, _user, visit_merchants, max_fail_count,
                                        getSilverAvailable(), getGoldAvailable());
                        // 貌似通商了就干不了其他了? 新版通商没有疲劳啊
                        if (result == 1 || result == 10) return next_halfhour();
                        else if (result == 2) return next_hour(); // not enough movable
                        else if (result == 0) return immediate();
                        else return next_halfhour();
                    }
                    else
                    {
                        result = mgr.handleTradeInfo(_proto, _logger, _user, out trade_state, false, false);
                        if (result == 0) return immediate();
                        else if (result == 3) return next_halfhour(); // not enough movable
                        else if (result == 10) return next_halfhour(); // other command error
                        else if (result == 2) continue; // work done, do nothing, continue to do
                    }
                }
                else if (thingid == 5)
                {
                    result = mgr.handleTradeInfo(_proto, _logger, _user, out trade_state, true, false);
                    if (result == 0) return immediate();
                    else if (result == 3) return next_halfhour(); // not enough movable
                    else if (result == 10) return next_halfhour(); // other command error
                    else if (result == 2) continue; // work done, do nothing, continue to do
                }
            }
            return next_hour();
        }
    }
}
